package view.GUI.adminView.invoiceOverview;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import enums.GUIText;
import model.Utility;
import model.data.tableModel.TableControl;
import model.data.tableModel.sale.Bill;
import model.handler.databaseHandling.QueryFactory;
import model.handler.databaseHandling.QueryFactory.QueryProperty;
import model.handler.fileHandling.FileHandler;
import model.invoicing.DateHandler;
import javafx.event.EventHandler;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;


public final class InvoiceHistory extends StackPane{

    private TreeView<String> tree;
	private List<TreeItem<String>> yearItemList;
	private TreeItem<String> rootItem;
	
	public InvoiceHistory(){
		yearItemList = new ArrayList<>();	
		createWidgets();
		rootItem = new TreeItem<>("Jahr");
		tree.setRoot(rootItem);
		addWidgets();
		setupLayout();
		setupInteraction();
	}
	
	public void addInvoice(String year, List<Bill> billList){
		TreeItem<String> yearItem = null;
		
		for(TreeItem<String> i: yearItemList){
			if(i.getValue().equals(year)){
				yearItem = i;
			}
		}
		if(yearItem == null){
			yearItem = new TreeItem<String> (year);
			yearItemList.add(yearItem);
		}
		rootItem.getChildren().add(yearItem);
		
		Date monthDate = null;
		TreeItem<String> monthItem = null;
		
		for(Bill b: billList){
			if(monthDate == null || !DateHandler.parseMonthDateStringToDate(b.getDate()).equals(monthDate)){
				if(monthDate != null){
					yearItem.getChildren().add(monthItem);
				}
				
				monthDate = DateHandler.parseMonthDateStringToDate(b.getDate());
				monthItem = new TreeItem<String>(DateHandler.getMonthFromDate(monthDate));
				monthItem.getChildren().add(new TreeItem<String>(GUIText.MONATSBILANZ.toString()));
				monthItem.getChildren().add(new TreeItem<String>(GUIText.LOHNKOSTENABRECHNUNG.toString()));
			}
			TreeItem<String> billItem = new TreeItem<String>(b.getCustomer().getName());
			monthItem.getChildren().add(billItem);
		}
		
		yearItem.getChildren().add(monthItem);
	}
	

	private void createWidgets() { 
		tree = new TreeView<String>();
	}

	private void addWidgets() {
		this.getChildren().add(tree);
	}

	private void setupLayout() {
		// TODO Auto-generated method stub
		
	}
	
	private void setupInteraction(){
		tree.setOnMouseClicked(new EventHandler<MouseEvent>()
				{
				    @Override
				    public void handle(MouseEvent mouseEvent)
				    {            
				        if(mouseEvent.getClickCount() == 2)
				        {
				        	if(tree.getSelectionModel().getSelectedItem().isLeaf()){
				        		File file = null;
				        		TreeItem<String> item = tree.getSelectionModel().getSelectedItem();
				        		TreeItem<String> month = item.getParent();
				        		TreeItem<String> year = month.getParent();
				        		
				        		String date = Utility.concatStrings(year.getValue(), "-", String.valueOf(DateHandler.formatStringMonthToNumber(month.getValue())));
				        		
				        		if(item.getValue().equals(GUIText.MONATSBILANZ.toString())){
				        			file = QueryFactory.getInvoice(QueryProperty.BALANCEINVOICE, date);
				        		} else if(item.getValue().equals(GUIText.LOHNKOSTENABRECHNUNG.toString())){
				        			file = QueryFactory.getInvoice(QueryProperty.SALARYINVOICE, date);
				        		} else{
				        			file = QueryFactory.getInvoiceFile(date , QueryFactory.getBillID(TableControl.getInstance().getCustomerMap().get(item.getValue()).getCustomerID(), date));				            	
				        		}				            
				        		FileHandler.openPdf(file);				        		
				        	}
				        }
				    }
				});
	}
}
